<!--使用“vb”快速生成页面 必须安装插件Vue VSCode Snippets-->
<template>
  <div class="login-box">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
    >
      <el-form-item label="账号" prop="user_name">
        <el-input v-model="ruleForm.user_name" type="text" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="ruleForm.password"
          type="password"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm(ruleFormRef)"
          class="loginBtn"
          >Submit</el-button
        >
        <el-button @click="resetForm(ruleFormRef)" class="loginBtn"
          >Reset</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs, ref } from "vue";
import { LoginData } from "../type/login";
import type { FormInstance } from "element-plus";
import { ElMessage } from 'element-plus';
import {tslogin} from "../api/tsApi"
import { useStore } from "vuex";
import {useRouter} from "vue-router"
export default defineComponent({
  setup() {
    /**
     * reactive函数定义一个对象类型的响应式数据（基本类型不能用它，需要使用ref函数）
     * 接收一个对象，返回一个代理对象（Proxy的实例对象，简称Proxy对象）
     * 内部基于Es6的proxy实现，通过代理对象操作源对象内部数据进行操作。
     */
    /*响应式数据*/
    const data = reactive(new LoginData());
    /*静态定义数据*/
    const rules = {
      username: [
        {
          required: true,
          message: "请输入账号",
          trigger: "blur",
        },
        {
          min: 3,
          max: 20,
          message: "账号的长度在3到20之间",
          trigger: "blur",
        },
      ],
      password: [
        {
          required: true,
          message: "请输入密码",
          trigger: "blur",
        },
        {
          min: 3,
          max: 10,
          message: "密码的长度在3到10之间",
          trigger: "blur",
        },
      ],
      // checkName: [{ validator: validateCheck, trigger: "blur" }],
      // checkPass: [{ validator: validateCheck, trigger: "blur" }],
    };
    const ruleFormRef = ref<FormInstance>();
    /*路由跳转*/
    const router = useRouter();
    /*vuex*/
    const store = useStore();
    /*登录事件*/
    const submitForm = (formEl: FormInstance | undefined) => {
      if (!formEl) return;
      //对表单内容进行验证
      formEl.validate((valid) => {
        if (valid) {
          tslogin(data.ruleForm).then(res=>{
            console.log(res["code"])
            // res.code = res.code ||
            if(res["code"]==1){
              /*这里后台没有token暂时给模拟的*/
              store.commit("userInfo/SET_TOKEN", '2fdbc7d3-51d3-4f39-a7ec-916f53c36f58');
              store.commit("userInfo/SET_USERNAME", res["data"][0]["username"]);
              store.commit("userInfo/SET_PASSWORD", res["data"][0]["password"]);
              store.commit("userInfo/SET_HEADPIC", res["data"][0]["head_pic"]);
              console.log(res["data"][0]["username"])
              router.push("/tsmain")
            }else{
              ElMessage.error(res["msg"])
            }
          })
          console.log("submit!");
        } else {
          console.log("error submit!");
          return false;
        }
      });
    };
    return { ...toRefs(data), rules, submitForm, ruleFormRef };
  },
});
</script>

<style lang="scss" scoped>
.login-box {
  width: 100%;
  height: 100%;
  background: url("../assets/back.jpg");
  padding: 1px;
  .demo-ruleForm {
    width: 500px;
    background-color: #fff;
    padding: 40px;
    margin: 20px auto;
    border-radius: 20px;
    .loginBtn {
      width: 48%;
    }
  }
}
</style>